/**
 * Created by ybf on 2017/8/5.
 */

var im_controller = null;
//view_controller
function view_controller() {
    //用户列表
    var talk_user_com = {
        template: '#user-list',
        props: ['user_list'],
        data: function () {
            return {}
        },
        methods: {
            go_info: function (val) {
                /*router.push({
                 path: 'talk_info',
                 params:{ user: 123}
                 });*/
                //log(val);
                val.unread = 0;
                router.push({path: 'talk_info/' + val.To_Account});
            }
        },
        filters: {
            format_time: function (val) {
                return new Date(parseInt(val) * 1000).toLocaleString().substr(0, 17);
            }
        }
    };

//对话详情
    var talk_info = {
        template: '#talk-info',
        //props: ['user_list'],
        mounted: function () {
            var _this = this;
            this.$nextTick(function () {
                im_controller.selToID = _this.$route.params.userId;
                getLastC2CHistoryMsgs(
                    im_controller.selToID,
                    function (list) {
                        //消息已读上报，以及设置会话自动已读标记
                        addHistoryMsgs(_this,list,1);
                        _this.$nextTick(function () {
                            scorll_bottom();
                        });
                    },
                    function () {
                        alert('消息获取失败');
                    }
                );
                im_controller.$on('add_new_msg',function (msg) {
                    _this.talk_info.push(msg);
                    _this.$nextTick(function () {
                        scorll_bottom();
                    });
                });
                //滚动
                _this.scroll_list();
            });
            //console.log(im_controller.$route.params.userId);
            //selSess = webim.MsgStore.sessByTypeId('C2C', im_controller.$route.params.userId);
            // selSess = webim.MsgStore.sessMap();
            // console.log(selSess);


        },
        data: function () {
            return {
                talk_info: new Array,
                edit_msg: '',
                selToID: '',
                scroll_lock: true
            }
        },
        methods: {
            send_msg:function () {
                if (this.edit_msg.length > 0){
                    onSendMsg(this.edit_msg);
                    this.edit_msg = '';
                    $("#textarea-msg").height(24);
                }else {
                    alert('请输入发送内容');
                }
            },
            scroll_list:function () {
                var _this = this;
                $(document).scroll(function () {
                    if ($(document).scrollTop() < 10){
                        if (!(_this.scroll_lock)){
                            return;
                        }
                        _this.scroll_lock = false;
                        getPrePageC2CHistoryMsgs(
                            _this,
                            im_controller.selToID,
                            function (list) {
                                _this.scroll_lock = true;
                                addHistoryMsgs(_this,list,0);

                                /*list = list.MsgList;
                                for (var i = list.length - 1; i >= 0; i--){
                                    addHistoryMsg(_this,list[i],'unshift');
                                    //log();
                                    /!*var m_obj = {};
                                    m_obj.content = list[i].getElems()[0].getContent().getText();
                                    m_obj.fromAccount = list[i].getFromAccount();
                                    m_obj.fromAccountNick = list[i].getFromAccountNick();
                                    m_obj.time = list[i].getTime();
                                    m_obj.isSend = list[i].getIsSend();
                                    _this.talk_info.unshift(m_obj);*!/
                                }*/
                            },
                            function () {
                                alert('消息获取失败');
                            }
                        );
                    }
                });
            }
        }
    };

//路由
    var routes = [
        {
            path: '/',
            component: talk_user_com
        },
        {
            path: '/talk_info/:userId',
            component: talk_info
        }
    ];

    var router = new VueRouter({
        routes: routes // （缩写）相当于 routes: routes
    });

    im_controller = new Vue({
        el: '#im-app',
        data: {
            talk_user_list: [],
            talk_info_list: [],
            selToID: '', //为当前聊天对象的消息
            new_msg: ''
        },
        router: router,
        methods: {
            im_get_msg: function (im_msg) {
                /*var sessMap = webim.MsgStore.sessMap();
                this.talk_user_list = sessMap;
                log(im_msg);
                log(sessMap);*/
            },
            get_new_msg: function (msg) {
                this.$emit('add_new_msg',msg);
            },
            set_unread: function (arr) {
                var sel_user = search_arr_val(this.talk_user_list,'To_Account',arr.id());
                if (sel_user){
                    this.$set(sel_user,'unread',arr.unread());
                }
            }
        }
    });
}


